#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "data.h"
#include "var.h"

ENV_VAR* pENV_TABLE=NULL;

void init_env_table(void) {
    pENV_TABLE=env_var_alloc();
    strcpy(pENV_TABLE->name,"HEAD");
    strcpy(pENV_TABLE->value,"HEAD");
    pENV_TABLE->next=NULL;
}

ENV_VAR *env_var_alloc(void) {
    return (ENV_VAR*)malloc(sizeof(ENV_VAR));
}
char *get_var_value(ENV_VAR* var) {
    return var?var->value:NULL;
}
ENV_VAR* get_var(char *name) {
    ENV_VAR* pvar=pENV_TABLE;
    while (pvar != NULL) {
        if (strcmp(pvar->name,name)==0) {
            return pvar;
        }
        pvar=pvar->next;
    }
    return NULL;
}
int save_env_var(const char *cmdline) {
    ENV_VAR* pvar=pENV_TABLE;
    ENV_VAR* tvar=env_var_alloc();
    ENV_VAR* tmp;
    tmp=pvar->next;
    pvar->next=tvar;
    tvar->next=tmp;
    char *p=strstr(cmdline,"=");
    strcpy(tvar->value,p+1);
    strncpy(tvar->name,cmdline,abs(cmdline-p));
    return 0;
}
